(function () {
    const loginBox = $('.loginBox')
    const registerBox = $('.registerBox')
    const infoBox = $('.infoBox')

    loginBox.on('click', 'a', function () {
        registerBox.show();
        loginBox.hide()
    })

    registerBox.on('click', 'a', function () {
        registerBox.hide();
        loginBox.show()
    })

    $('.registerBtn').click(function () {
        $.ajax({
            url: "/api/user/register",
            type: 'post',
            data: {
                username: registerBox.find('[name=username2]').val(),
                password: registerBox.find('[name=password2]').val(),
                repassword: registerBox.find('[name=qrPwd2]').val(),
            },
            dataType: "json",
            success: function (res) {
                console.log(res);
                $(".registerTip").text(res.msg).show();
                console.log($(".registerTip"));
                if (res.code == 2) {
                    setTimeout(function () {
                        registerBox.hide();
                        loginBox.show()
                    }, 3000)
                }
            }
        })
    })

    $('.loginBtn').click(function () {
        $.ajax({
            url: "/api/user/login",
            type: 'post',
            data: {
                username: loginBox.find('[name=username]').val(),
                password: loginBox.find('[name=password]').val(),
            },
            dataType: "json",
            success: function (res) {
                console.log(res);
                $(".loginTip").text(res.msg).show();
                if (res.code == 0) {
                    // setTimeout(function () {
                    //     infoBox.show()
                    //     loginBox.hide()
                    // }, 3000)
                     window.location.reload()
                }
            }
        })
    })

    //
    $('.logout').click(function () {
        console.log('ddddd')
        $.ajax({
            url: "/api/user/logout",
            type: 'get',
            success: function (res) {
                if(res.code==0){
                    window.location.reload()
                }
            }
        })
    })
})()